package projectg06.estructuras;

import java.util.Arrays;
import static java.lang.Math.*;

public class VecMath {
	private double[] vec;
	
	public VecMath(int d){
		this.vec= new double[d];
	}
	public VecMath(double[] valores){
		this.vec= new double[valores.length];
		for (int i=0; i<valores.length; i++) {
			this.vec[i]=valores[i];
		}		
	}
	public VecMath(String pathValores){
		//Completar		
	}
	
	@Override
	public String toString() {
		return "VecMath [vec=" + Arrays.toString(vec) + "]";
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		VecMath other = (VecMath) obj;
		if (!Arrays.equals(vec, other.vec))
			return false;
		return true;
	}
	
	public VecMath sumar(VecMath v2) throws Exception{
		if(v2.retSize()!=this.retSize()){
			throw new Exception("Distintas dimensiones.");
		}
		double[] val = new double[this.vec.length];
		for (int i = 0; i < this.vec.length; i++) {
			val[i]=this.vec[i] + v2.vec[i];
		}
		return new VecMath(val);
	}
	
	public VecMath restar(VecMath v2) throws Exception{
		if(v2.retSize()!=this.retSize()){
			throw new Exception("Distintas dimensiones.");
		}
		double[] val = new double[this.vec.length];
		for (int i = 0; i < this.vec.length; i++) {
			val[i]=this.vec[i] - v2.vec[i];
		}
		return new VecMath(val);
	}
			
	public int retSize() {
		return this.vec.length;
	}
	
	public double norma(){
		double sumas=0;
		for (int i = 0; i < this.vec.length; i++) {
			sumas +=pow(this.vec[i],2);
		}
		return sqrt(sumas);
	}
	
	public static void main(String[] args) throws Exception {	
		// Test constructores
		VecMath v1 = new VecMath(3);
		VecMath v2 = new VecMath(new double[]{1,2,3,4,5});
		VecMath v3 = new VecMath(new double[]{1,2,3,4,5});
		//Test toString()
		System.out.println("V1:"+v1);
		System.out.println("V2:"+v2);
		System.out.println("V3:"+v3);
		//Test equals
		System.out.println("V2 respecto de V1 deberia ser false: " + v2.equals(v1));
		System.out.println("V2 respecto de V3 deberia ser true: " + v2.equals(v3));
		//Test Suma
		System.out.println("Suma V2,V3-> "+v2.sumar(v3));
		//Test Resta
		System.out.println("Resta V2,V3-> "+v2.restar(v3));
		//Test Norma
		VecMath v4 = new VecMath(new double[]{1,1,1,1});
		System.out.println("Norma " + v4 + " se espera 2 -> "+v4.norma());
	}
}
